package org.noote.test_camera_broadcast;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.noote.libs.camera.CameraInterface;
import org.noote.libs.camera.broadcast.CameraBroadcast;
import org.noote.libs.camera.broadcast.CameraClient;
import org.noote.libs.camera.helper.Helper_Camera;
import org.noote.libs.camera.preview.CameraPreview;
import org.noote.libs.tcpip.helper.Helper_WiFi;

public class TestCameraBroadcast_Activity extends Activity {
	private static final String LOG_TAG = "TestCameraServer_Activity";
	
	private CameraInterface mCamera = new CameraInterface();
	private CameraPreview mPreview = null;
	private CameraBroadcast mBroadcast = null;
	private CameraClient mClient = null;
	private String m_sCurrentIPAddress = "0.0.0.0";
	private int m_iServerPort = 8088;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		getCurrentIPAddress();
		
		TextView textViewServer = (TextView) findViewById(R.id.textViewServer);
		textViewServer.setText("Server on "+m_sCurrentIPAddress+":"+m_iServerPort);

		if(Helper_Camera.checkCameraHardware(getApplicationContext()))
		{
			/** create camera */
			if(!mCamera.open(Helper_Camera.getCamera()))
			{
				Toast.makeText(getApplicationContext(), "Cannot start camera !", Toast.LENGTH_LONG).show();
				return;
			}

			mPreview = new CameraPreview(this, mCamera);
			((FrameLayout) findViewById(R.id.layoutPreview)).addView(mPreview);
			
			mBroadcast = new CameraBroadcast(this, mCamera);

			// Launch IP server
	    	Log.d(LOG_TAG, "Launch ip server on "+m_sCurrentIPAddress+":"+m_iServerPort+"...");
	    	if(!mBroadcast.createServer(m_iServerPort))
	    	{
	    		Log.d(LOG_TAG, "Error then start server :(");
	    		Toast.makeText(this, "Error then start server :(", Toast.LENGTH_LONG).show();
	    	}		
		}
		else
		{
			Toast.makeText(getApplicationContext(), "Your device have no camera !", Toast.LENGTH_LONG).show();
		}

		mClient = new CameraClient(this);
		((FrameLayout) findViewById(R.id.layoutClient)).addView(mClient);

		FrameLayout layoutClient = (FrameLayout) findViewById(R.id.layoutClient);
		layoutClient.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
				if(!mClient.isConnected())
					mClient.connect(m_sCurrentIPAddress, m_iServerPort);
				mClient.askJpegPreview(30);
			}
        });
		
	}
	
	protected void onStart()
	{
		super.onStart();
	}
	
	protected void onResume()
	{
		super.onResume();
		
		if(mCamera != null)
			mCamera.startPreview();
	}
	
	protected void onPause()
	{
		if(mCamera != null)
			mCamera.stopPreview();
		
		super.onPause();
	}
	
	protected void onStop()
	{
		mClient.disconnect();
		mBroadcast.killServer();
		
		super.onStop();
	}
	
	protected void onDestroy()
	{
		super.onDestroy();
	}
	
	private void getCurrentIPAddress() {
		m_sCurrentIPAddress = Helper_WiFi.getCurrentIPAddress_WiFi_Only(this, true);
		if(m_sCurrentIPAddress == "")
		{
			new AlertDialog.Builder(this)
			.setTitle("Not WiFi active connexion detected...")
			.setPositiveButton("Ok",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog,
								int whichButton) {
							finish();
						}
					}).show();
			return;
		}
	}
}
